class PlayersController < ApplicationController
  def index
  	@team = Team.find(params[:team_id])
  	@players = @team.players
  end

  def show
  	@player = Player.find(params[:id])
  end

  def new
  	@team = Team.find(params[:team_id])
  	@player = @team.players.build
  end

  def edit
  	@team = Team.find(params[:team_id])
  	@player = @team.players.find(params[:id])
  end

  def update
    @team = Team.find(params[:team_id])
    @player = Player.find(params[:id])

    if @player.update_attributes(params[:player])
      flash[:notice] = "%s updated" % @player.name 
      redirect_to team_players_url(@team)
    else
      flash[:notice] = "Error while updating the player"
      redirect_to :action => :edit
    end
  end

  def create
    @team = Team.find(params[:team_id])
    @player = @team.players.build(params[:player])

    if @player.save
      flash[:notice] = "%s added to the team" % @player.name 
      redirect_to team_players_url(@team)
    else
      flash[:notice] = "Error while saving the new player"
      redirect_to :action => :new
    end
  end

end
